<template>
	<view class="container">
		<uni-forms ref="form" :rules="{}">
			<uni-forms-item label="用户名" name="">
				<uni-easyinput placeholder="请输入用户名" />
			</uni-forms-item>

			<uni-forms-item label="密码" name="">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>

			<uni-forms-item label="学院" name="">
				<uni-combox :candidates="departmentList" v-model="user.department" placeholder="请选择学院"
					@change="departmentChange" />
				</uni-combox>
			</uni-forms-item>

			<uni-forms-item label="专业" name="">
				<uni-combox :candidates="majorList" v-model="user.major" placeholder="请选择专业" @change="majorChange" />
				</uni-combox>
			</uni-forms-item>

			<uni-forms-item label="电话" name="">
				<uni-easyinput v-model="user.telephone" @change="telephoneChange" placeholder="请输入电话" />
			</uni-forms-item>

			<uni-forms-item label="姓名" name="">
				<uni-easyinput v-model="user.name" @change="nameChange" placeholder="请输入姓名" />
			</uni-forms-item>

			<uni-forms-item label="邮箱" name="">
				<uni-easyinput v-model="user.email" @change="emailChange" placeholder="请输入邮箱" />
			</uni-forms-item>

			<uni-forms-item label="" name="">
				<button type="primary" @click="register">注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '',
					password: '',
					email: '',
					telephone: '',
					department: '',
					major: ''
				},
				departmentList: ['信息工程', '智能制造', '经济管理', '文法'],
				majorList: ['大数据', '移动应用开发', '数字媒体', '会计']
			}
		},
		methods: {
			telephoneChange(e) {
				this.user.telephone = e;
			},
			nameChange(e) {
				this.user.name = e;
			},
			emailChange(e) {
				this.user.email = e;
			},
			departmentChange(e) {
				this.user.department = e;
				console.log('选择的学院:', this.user.department);
			},
			majorChange(e) {
				this.user.major = e;
				console.log('选择的专业:', this.user.major);
			},
			register(e) {
				if (!this.user.name) {
					uni.showToast({
						title: '您的姓名不能为空',
						icon: 'none'
					});
					return;
				}
				if (!this.user.password) {
					uni.showToast({
						title: '您的密码不能为空',
						icon: 'none'
					});
					return;
				}

				uni.request({
						url: 'http://localhost:8090/freshmenapp/user/register',
						method: 'POST',
						data: this.user,
						success: res => {
							console.log(e);
							if (res.data.success == true) {
								uni.showToast({
									title: '注册成功'
								});
							} else {
								uni.showToast({
									title: '失败',
									icon: 'none'
								});
							}
						},
						fail: () => {
							uni.showToast({
								title: '真糟糕！您的网络被外星人偷走了！！！',
								icon: 'none'
							});
						},
						complete: () => {}
					}),
					uni.showToast({
						title: '' + this.user.username + '\t' + this.user.password + '\t' +
							this.user.department + '\t' + this.user.major + '\t' +
							this.user.telephone + '\t' + this.user.name + '\t' +
							this.user.email,
						icon: 'none'
					});
			}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
